package weaver.formmode.customjavacode;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;


/**
 *员工宿舍登记保存后触发同步信息到宿舍信息建模
 * @author Lee
 */
public class DormitorySaveExpand extends AbstractModeExpandJavaCodeNew {
    private static String formModeId = "28";
    private static String formCusId = "35";

    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            RecordSet rs = new RecordSet();
            int billid = -1;
            int modeid = -1;
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if (billid > 0 && modeid > 0) {
                    String sql = "select a.id,a.rzss,a.xm,a.rzsj,a.tssj,b.id as mainid from uf_ygzsmx a " +
                            "left join uf_ssxxwh b  on b.ssbh=a.rzss " +
                            "where a.id=?";
                    rs.executeQuery(sql, billid);
                    if (rs.next()) {
                        //员工住宿登记表id
                        String id = rs.getString("id");
                        String rzss = rs.getString("rzss");
                        String xm = rs.getString("xm");
                        String rzsj = rs.getString("rzsj");
                        String tssj = rs.getString("tssj");
                        String mainid = rs.getString("mainid");
                        //查询是否已经在宿舍信息里了
                        sql = "select b.id from uf_ssxxwh a left join uf_ssxxwh_dt1 b on b.mainid=a.id where a.ssbh=? and b.flagid=?";
                        rs.executeQuery(sql, rzss, id);
                        if (rs.next()) {
                            String detailId = rs.getString("id");
                            //更新到宿舍明细表
                            sql = "update uf_ssxxwh_dt1 set xm=?,rzsj=?,tssj=? where id=?";
                            rs.executeUpdate(sql, xm, rzsj, tssj, detailId);
                        } else {
                            //插一个人到宿舍信息明细表
                            sql = "insert into uf_ssxxwh_dt1 (mainid,xm,rzsj,tssj,flagid) values (?,?,?,?,?)";
                            rs.executeUpdate(sql, mainid, xm, rzsj, tssj, id);
                        }

                        //计算已住人数
                        sql = "select count(b.id) as total from uf_ssxxwh a " +
                                "left join uf_ssxxwh_dt1 b on b.mainid=a.id " +
                                "where a.ssbh=" + rzss + " and (b.tssj is null or b.tssj='' or b.tssj>='" + TimeUtil.getCurrentDateString() + "')";
                        rs.executeQuery(sql);
                        if (rs.next()) {
                            String total = rs.getString("total");
                            //更新主表已住人数
                            sql = "update uf_ssxxwh set yzrs=? where id=?";
                            rs.executeUpdate(sql, total, mainid);
                            //权限重构
                            ModeRightInfo modeRightInfo = new ModeRightInfo();
                            modeRightInfo.setNewRight(true);
                            modeRightInfo.editModeDataShare(1, Integer.parseInt(formModeId), Integer.parseInt(formCusId));
                        }
                    }
                }
            }
        } catch (Exception e) {
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

}